function treeToArray(tree, id = 'id', pid = 'pid') {
    let res = []
    for (const item of tree) {
        const { children, ...i } = item
        if (children && children.length) {
            children.forEach(element => {
                element[pid] = item[id];
            });
            res = res.concat(treeToArray(children))
        }
        res.push(i)
    }
    return res
}
console.log(treeToArray([{ "id": 1, "children": [{ "id": 2, "children": [{ "id": 3 }] }] }]))